package org.jugile.util;

/*

Copyright (C) 2007-2011 Jukka Rahkonen  email: jukka.rahkonen@iki.fi

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * "I am Alpha and Omega, the beginning and the ending, saith the Lord, 
 *  which is, and which was, and which is to come, the Almighty." (Rev 1:8)
 * 
 * ==========
 * 
 * Convinience wrapper around W3C Node.
 * 
 * @author jukka.rahkonen@iki.fi
 */
public class DomNode {

	private Element elem;
	private DomNode(Element e) {
		this.elem = e;
	}

	public DomNode(String data) {
		Document dom = null;
		try {
			DocumentBuilder domb = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			byte[] bytes = data.getBytes("UTF-8");
		    InputStream is = new ByteArrayInputStream(bytes);
			dom = domb.parse(is);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		elem = dom.getDocumentElement();
	}
	
	public DomNode(File file) {
		Document dom = null;
		try {
			DocumentBuilder domb = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			dom = domb.parse(file);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		elem = dom.getDocumentElement();
	}
	
	public DomNode(Document doc) {
		elem = doc.getDocumentElement();
	}
	
	
	public List<DomNode> getAll(String name) {
		List<DomNode> res = new ArrayList<DomNode>();
		NodeList nodes = elem.getElementsByTagName(name);
		for (int i = 0; i < nodes.getLength(); i++) {
			if (! (nodes.item(i) instanceof Element)) continue;
			Element e = (Element)nodes.item(i);
			res.add(new DomNode(e));
		}
		return res;
	}
	
	public DomNode get(String name) {
		List<DomNode> items = getAll(name);
		if (items.size() == 0) return null;
		return items.get(0);
	}
	
	public DomNode get(String tag, String attr, String value) {
		for (DomNode e : getAll(tag)) {
			if (value.equals(e.attr(attr))) return e;
		}
		return null;
	}

	public String attr(String name) {
		return elem.getAttribute(name);
	}
	
	public String text() {
		StringBuffer res = new StringBuffer();
		NodeList nl = elem.getChildNodes();
		if (nl == null) return "";
		for (int i = 0; i < nl.getLength(); i++) {
			Node n = nl.item(i);
			if (n.getNodeType() == Node.TEXT_NODE) {
				res.append(n.getNodeValue());
			}
		}
		return res.toString();
	}

		
}
